#!/bin/bash
# shellcheck source=meta-facebook/recipes-fb/obmc_functions/files/fb-common-functions
source /usr/libexec/fb-common-functions

# 88E6393X PWRGD gpio number
CHIP_PWRGD=100

set_bmc_ready()
{
    local bmc_ready="/sys/class/leds/bmc_ready_noled/brightness"
    echo 1 > ${bmc_ready}

    return 0
}

# Check if both CPLDs are available.
check_cpld_state()
{
    output=$(i2ctransfer -y 14 w4@0x40 0xE0 0x00 0x00 0x00 r4 2>/dev/null)
    dev_id="21 2e 30 43"

    response=$(echo "$output" | grep -oE '0x[0-9a-fA-F]{2}' | sed 's/0x//' | tr 'A-F' 'a-f' | xargs)
    if [[ "$response" != "$dev_id" ]]; then
        log-create xyz.openbmc_project.State.SMC.SMCFailed --json \
        "{\"IDENTIFIER\": \"/xyz/openbmc_project/software/chassis/Ventura_RMC_cpld\", \"FAILURE_TYPE\": \"Critical\"}"
    fi
    return 0
}

#Set fixed PWM to 80% before power brick.
set_fan_pwm_fixed()
{
    local mainsource
    mainsource=$(i2cget -y -f 18 0x20)

    if [[ -n "$mainsource" && "$mainsource" =~ ^0[xX][0-9a-fA-F]+$ ]]; then
        #Set fixed max31790 PWM to 80%
        echo $((255 * 80/100 )) >/sys/class/i2c-dev/i2c-18/device/18-0020/hwmon/hwmon*/pwm1
        echo $((255 * 80/100 )) >/sys/class/i2c-dev/i2c-18/device/18-0020/hwmon/hwmon*/pwm3
        echo $((255 * 80/100 )) >/sys/class/i2c-dev/i2c-18/device/18-0020/hwmon/hwmon*/pwm4
        echo $((255 * 80/100 )) >/sys/class/i2c-dev/i2c-18/device/18-0020/hwmon/hwmon*/pwm6
        sleep 2
    else
       #Set fixed nct7363 PWM to 80%
       echo $((255 * 80/100 )) >/sys/class/i2c-dev/i2c-18/device/18-0023/hwmon/hwmon*/pwm1
       echo $((255 * 80/100 )) >/sys/class/i2c-dev/i2c-18/device/18-0023/hwmon/hwmon*/pwm5
       echo $((255 * 80/100 )) >/sys/class/i2c-dev/i2c-18/device/18-0023/hwmon/hwmon*/pwm7
       echo $((255 * 80/100 )) >/sys/class/i2c-dev/i2c-18/device/18-0023/hwmon/hwmon*/pwm11
       sleep 2
    fi
}

config_fan_max31790()
{
    # Set fan tack sample count for each tach channel to highest value to avoid
    # wrong fan tach reading.
    #
    # FanX_Dynamics
    # - register offset: 0x08 (tach1) - 0x0d (tach6)
    # |------------------------ -----------------------------------------------|
    # | bit[7:5]         | 000b | 001b | 010b | 011b | 110b | 101b, 110b, 111b |
    # |------------------------------------------------------------------------|
    # | tach sample count|  1   |  2   |  4   |  8   |  16  |  32              |
    # |------------------------------------------------------------------------|
    addr=0x20

    for offset in $(seq 0x08 0x0d)
    do
        fan_dynam_val=$(printf "%d" "$(i2cget -y -f 18 "$addr" "$offset")")
        fan_dynam_val=$((fan_dynam_val | 0xe0))
        i2cset -y -f 18 "$addr" "$offset" "$fan_dynam_val"
    done

    # after manual set FanX_Dynamics, needs to rebind driver to ensure
    # driver get correct setitngs
    bus_addr="$(printf "18-%04x" $addr)"
    echo "$bus_addr" > /sys/bus/i2c/drivers/max31790/unbind
    echo "$bus_addr" > /sys/bus/i2c/drivers/max31790/bind

    # set fan enable
    if find /sys/bus/i2c/devices/*/hwmon/*/fan*_enable -print -quit 2>/dev/null | grep -q .; then
        for file in /sys/bus/i2c/devices/*/hwmon/*/fan*_enable
        do
            echo 1 > "${file}"
        done
    fi
}

config_fan()
{
    if [ -d "/sys/bus/i2c/drivers/max31790/18-0020/hwmon" ]; then
        echo "Configure MAX31790 fan controller"
        config_fan_max31790
    fi
}

power_on_led_init() {
    systemctl start obmc-led-group-start@power_on.service
}

mdio_LED_setting_for_marvell_chip()
{
    ## To configure the SFP+ LED to blink based on Link/Act/Speed
    # LED 0 & 1 Control for Port 9
    led_port9=$(mdio 1* 10 0x16)

    # Set the 15th bit to 1 for updating
    led_port9=$((led_port9 | 0x8000))

    # Clear the 7~4th bits for Port 10 Link/Act/Speed by blinking
    led_port9=$((led_port9 & ~0x00F0))

    # Write to the register
    mdio 1* 10 0x16 $led_port9
}

# Change the i2c path to the eeprom to the 88E6393X
set_gpio FM_88E6393X_BIN_UPDATE_EN_N 1
# Sleep 2s
sleep 2s
# Enable the pwrgd to the 88E6393X
# Temp setting by gpio group and number directly, need to discuss whether need to define in the DTS (long term solution)
gpioset 0 "$CHIP_PWRGD"=1

set_fan_pwm_fixed
set_bmc_ready
power_on_led_init
mdio_LED_setting_for_marvell_chip

# Set fan target and enable all fan input as early as possible
config_fan
check_cpld_state

exit 0
